package org.minuframework.encrypt;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

import org.minuframework.exception.ExceptionFailed;
import org.minuframework.lang.StringUtils;

@Deprecated
public class BASE64U {
    
    public static final String DEFAULT_DATA_PREFFIX = "data:image/jpeg;base64,";
    public static final String UTF8                 = "UTF-8";
    private String             charset;
    
    public BASE64U setCharset(String charset) {
        this.charset = charset;
        return this;
    }
    
    // ------------------------------------------------------------------------
    
    // ------------------------------------------------------------------------
    
    public byte[] base64Encoder(byte[] bytes) {
        return Base64.getEncoder().encode(bytes);
    }
    
    public static byte[] base64Enc(byte[] bytes) {
        return new Base64Utils().setCharset(UTF8).base64Encoder(bytes);
    }
    
    public static byte[] base64EncSafe(byte[] bytes) {
        return base64Enc(bytes);
    }
    
    public byte[] base64Encoder(String str) throws UnsupportedEncodingException {
        return Base64.getEncoder().encode(str.getBytes(charset));
    }
    
    public static byte[] base64Enc(String str) throws UnsupportedEncodingException {
        return new Base64Utils().setCharset(UTF8).base64Encoder(str);
    }
    
    public static byte[] base64EncSafe(String str) {
        try {
            return base64Enc(str);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
    
    // ------------------------------------------------------------------------
    
    public String base64EncoderToString(byte[] bytes) {
        String encoded = Base64.getEncoder().encodeToString(bytes);
        return encoded;
    }
    
    public static String base64EncToString(byte[] bytes) {
        return new Base64Utils().setCharset(UTF8).base64EncoderToString(bytes);
    }
    
    public String base64EncoderToString(String str) throws UnsupportedEncodingException {
        String encoded = Base64.getEncoder().encodeToString(str.getBytes(charset));
        return encoded;
    }
    
    public static String base64EncToString(String str) throws UnsupportedEncodingException {
        return new Base64Utils().setCharset(UTF8).base64EncoderToString(str);
    }
    
    public static String base64EncToStringSafe(String str) {
        try {
            return base64EncToString(str);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
    
    // ------------------------------------------------------------------------
    
    // ------------------------------------------------------------------------
    
    public byte[] base64Decoder(byte[] bytes) {
        return Base64.getDecoder().decode(bytes);
    }
    
    public static byte[] base64Dec(byte[] bytes) {
        return new Base64Utils().base64Decoder(bytes);
    }
    
    public byte[] base64Decoder(String str) {
        return Base64.getDecoder().decode(str);
    }
    
    public static byte[] base64Dec(String str) {
        return new Base64Utils().base64Decoder(str);
    }
    
    // ------------------------------------------------------------------------
    
    public String base64DecoderToString(byte[] bytes) {
        return new String(base64Decoder(bytes));
    }
    
    public static String base64DecToString(byte[] bytes) {
        return new Base64Utils().base64DecoderToString(bytes);
    }
    
    public String base64DecoderToString(String str) {
        byte[] decoder = Base64.getDecoder().decode(str);
        return new String(decoder);
    }
    
    public static String base64DecToString(String str) {
        return new Base64Utils().base64DecoderToString(str);
    }
    
    // ------------------------------------------------------------------------
    
    // ------------------------------------------------------------------------
    
    public static byte[] getImgBytes(File file) throws ExceptionFailed {
        byte[] data = null;
        
        if (file.exists()) {
            try {
                InputStream in = new FileInputStream(file);
                data = new byte[in.available()];
                in.read(data);
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            throw new ExceptionFailed("Cannot find file from " + file);
        }
        
        return base64Dec(data);
    }
    
    public static String getImgStr(File file) throws ExceptionFailed {
        byte[] data = null;
        
        if (file.exists()) {
            try {
                InputStream in = new FileInputStream(file);
                data = new byte[in.available()];
                in.read(data);
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            throw new ExceptionFailed("Cannot find file from " + file);
        }
        
        return base64DecToString(data);
    }
    
    public static boolean generateImage(byte[] imgstr, File file) {
        boolean flag = false;
        if (imgstr == null || imgstr.length <= 0) { return flag; }
        
        try {
            byte[] bytes = base64Dec(imgstr);
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] < 0) { // 调整异常数据
                    bytes[i] += 256;
                }
            }
            // 生成jpeg图片
            OutputStream out = new FileOutputStream(file);
            out.write(bytes);
            out.flush();
            out.close();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return flag;
    }
    
    public static boolean generateImage(String imgstr, File file) {
        boolean flag = false;
        if (StringUtils.isEmpty(imgstr)) { return flag; }
        
        try {
            imgstr = replacePreffix(imgstr);
            byte[] bytes = base64Dec(imgstr);
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] < 0) { // 调整异常数据
                    bytes[i] += 256;
                }
            }
            // 生成jpeg图片
            OutputStream out = new FileOutputStream(file);
            out.write(bytes);
            out.flush();
            out.close();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return flag;
    }
    
    // ------------------------------------------------------------------------
    
    public static byte[] getImgBytes(String file) {
        byte[] data = null;
        
        try {
            InputStream in = new FileInputStream(file);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return base64Dec(data);
    }
    
    public static String getImgStr(String file) {
        byte[] data = null;
        
        try {
            InputStream in = new FileInputStream(file);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return base64EncToString(data);
    }
    
    public static boolean generateImage(byte[] imgstr, String file) {
        boolean flag = false;
        if (imgstr == null || imgstr.length <= 0) { return flag; }
        
        try {
            byte[] bytes = base64Dec(imgstr);
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] < 0) { // 调整异常数据
                    bytes[i] += 256;
                }
            }
            // 生成jpeg图片
            OutputStream out = new FileOutputStream(file);
            out.write(bytes);
            out.flush();
            out.close();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return flag;
    }
    
    public static boolean generateImage(String imgstr, String file) {
        boolean flag = false;
        if (StringUtils.isEmpty(imgstr)) { return flag; }
        
        try {
            imgstr = replacePreffix(imgstr);
            byte[] bytes = base64Dec(imgstr);
            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] < 0) { // 调整异常数据
                    bytes[i] += 256;
                }
            }
            // 生成jpeg图片
            OutputStream out = new FileOutputStream(file);
            out.write(bytes);
            out.flush();
            out.close();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return flag;
    }
    
    public static String replacePreffix(String imgstr) {
        return imgstr.split(",")[1];
    }
    
    public static String suffix(String imgstr) {
        if (StringUtils.isEmpty(imgstr)) { return null; }
        try {
            return '.' + imgstr.split(",")[0].split(";")[0].split("/")[1];
        } catch (Exception e) {
            return null;
        }
    }
    
    public static String suffix(byte[] buffer) {
        if (buffer.length <= 0) { return null; }
        try {
            return '.' + new String(buffer).split(",")[0].split(";")[0].split("/")[1];
        } catch (Exception e) {
            return null;
        }
    }
    
}
